.\" Copyright (c) 2016 Michael Kerrisk <mtk.manpages@gmail.com>
.\"
.\" %%%LICENSE_START(VERBATIM)
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.  The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\" %%%LICENSE_END
.\"
.TH PTHREAD_GETATTR_DEFAULT_NP 3 2021-03-22 "Linux" "Linux Programmer's Manual"
.SH NAME
pthread_getattr_default_np, pthread_setattr_default_np, \-
get or set default thread-creation attributes
.SH SYNOPSIS
.nf
.BR "#define _GNU_SOURCE" "             /* See feature_test_macros(7) */"
.B #include <pthread.h>
.PP
.BI "int pthread_getattr_default_np(pthread_attr_t *" attr );
.BI "int pthread_setattr_default_np(const pthread_attr_t *" attr );
.PP
Compile and link with \fI\-pthread\fP.
.fi
.SH DESCRIPTION
The
.BR pthread_setattr_default_np ()
function sets the default attributes used for creation of a new
thread\(emthat is, the attributes that are used when
.BR pthread_create (3)
is called with a second argument that is NULL.
The default attributes are set using the attributes supplied in
.IR *attr ,
a previously initialized thread attributes object.
Note the following details about the supplied attributes object:
.IP * 3
The attribute settings in the object must be valid.
.IP *
The
.IR "stack address"
attribute must not be set in the object.
.IP *
Setting the
.IR "stack size"
attribute to zero means leave the default stack size unchanged.
.PP
The
.BR pthread_getattr_default_np ()
function initializes the thread attributes object referred to by
.I attr
so that it contains the default attributes used for thread creation.
.SH ERRORS
.TP
.B EINVAL
.RB ( pthread_setattr_default_np ())
One of the attribute settings in
.IR attr
is invalid, or the stack address attribute is set in
.IR attr .
.TP
.B ENOMEM
.\" Can happen (but unlikely) while trying to allocate memory for cpuset
.RB ( pthread_setattr_default_np ())
Insufficient memory.
.SH VERSIONS
These functions are available in glibc since version 2.18.
.SH ATTRIBUTES
For an explanation of the terms used in this section, see
.BR attributes (7).
.ad l
.nh
.TS
allbox;
lbx lb lb
l l l.
Interface	Attribute	Value
T{
.BR pthread_getattr_default_np (),
.BR pthread_setattr_default_np ()
T}	Thread safety	MT-Safe
.TE
.hy
.ad
.sp 1
.SH CONFORMING TO
These functions are nonstandard GNU extensions;
hence the suffix "_np" (nonportable) in their names.
.SH EXAMPLES
The program below uses
.BR pthread_getattr_default_np ()
to fetch the default thread-creation attributes and then displays
various settings from the returned thread attributes object.
When running the program, we see the following output:
.PP
.in +4n
.EX
$ \fB./a.out\fP
Stack size:          8388608
Guard size:          4096
Scheduling policy:   SCHED_OTHER
Scheduling priority: 0
Detach state:        JOINABLE
Inherit scheduler:   INHERIT
.EE
.in
.SS Program source
\&
.EX
#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#define errExitEN(en, msg) \e
                        do { errno = en; perror(msg); \e
                             exit(EXIT_FAILURE); } while (0)

static void
display_pthread_attr(pthread_attr_t *attr)
{
    int s;
    size_t stacksize;
    size_t guardsize;
    int policy;
    struct sched_param schedparam;
    int detachstate;
    int inheritsched;

    s = pthread_attr_getstacksize(attr, &stacksize);
    if (s != 0)
        errExitEN(s, "pthread_attr_getstacksize");
    printf("Stack size:          %zd\en", stacksize);

    s = pthread_attr_getguardsize(attr, &guardsize);
    if (s != 0)
        errExitEN(s, "pthread_attr_getguardsize");
    printf("Guard size:          %zd\en", guardsize);

    s = pthread_attr_getschedpolicy(attr, &policy);
    if (s != 0)
        errExitEN(s, "pthread_attr_getschedpolicy");
    printf("Scheduling policy:   %s\en",
            (policy == SCHED_FIFO) ? "SCHED_FIFO" :
            (policy == SCHED_RR) ? "SCHED_RR" :
            (policy == SCHED_OTHER) ? "SCHED_OTHER" : "[unknown]");

    s = pthread_attr_getschedparam(attr, &schedparam);
    if (s != 0)
        errExitEN(s, "pthread_attr_getschedparam");
    printf("Scheduling priority: %d\en", schedparam.sched_priority);

    s = pthread_attr_getdetachstate(attr, &detachstate);
    if (s != 0)
        errExitEN(s, "pthread_attr_getdetachstate");
    printf("Detach state:        %s\en",
            (detachstate == PTHREAD_CREATE_DETACHED) ? "DETACHED" :
            (detachstate == PTHREAD_CREATE_JOINABLE) ? "JOINABLE" :
            "???");

    s = pthread_attr_getinheritsched(attr, &inheritsched);
    if (s != 0)
        errExitEN(s, "pthread_attr_getinheritsched");
    printf("Inherit scheduler:   %s\en",
            (inheritsched == PTHREAD_INHERIT_SCHED) ? "INHERIT" :
            (inheritsched == PTHREAD_EXPLICIT_SCHED) ? "EXPLICIT" :
            "???");
}

int
main(int argc, char *argv[])
{
    int s;
    pthread_attr_t attr;

    s = pthread_getattr_default_np(&attr);
    if (s != 0)
        errExitEN(s, "pthread_getattr_default_np");

    display_pthread_attr(&attr);

    exit(EXIT_SUCCESS);
}
.EE
.SH SEE ALSO
.ad l
.nh
.BR pthread_attr_getaffinity_np (3),
.BR pthread_attr_getdetachstate (3),
.BR pthread_attr_getguardsize (3),
.BR pthread_attr_getinheritsched (3),
.BR pthread_attr_getschedparam (3),
.BR pthread_attr_getschedpolicy (3),
.BR pthread_attr_getscope (3),
.BR pthread_attr_getstack (3),
.BR pthread_attr_getstackaddr (3),
.BR pthread_attr_getstacksize (3),
.BR pthread_attr_init (3),
.BR pthread_create (3),
.BR pthreads (7)
.SH COLOPHON
This page is part of release 5.13 of the Linux
.I man-pages
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
\%https://www.kernel.org/doc/man\-pages/.
